<template>
 <div >
    <van-nav-bar
      title="文件差分"
    />
    <van-cell-group>
      <van-cell title="AppType" :value="appType" @click="show=true" />
      <van-field
        v-model="buildType"
        required
        clearable
        input-align="right"
        label="BuildType"
      />
      <van-field
        v-model="fromVersionName"
        required
        clearable
        input-align="right"
        label="fromVersionName"
      />
      <van-field
        v-model="fromVersionCode"
        required
        clearable
        input-align="right"
        label="fromVersionCode"
      />
      <van-field
        v-model="versionName"
        required
        clearable
        input-align="right"
        label="VersionName"
      />
      <van-field
        v-model="versionCode"
        required
        clearable
        input-align="right"
        label="versionCode"
      />
    </van-cell-group>
    <van-actionsheet v-model="show" :actions="actions" />

    <van-button  :disabled="!submitEnable" class="mtop10" size="large" type="primary" :loading="loading" @click="save">保存</van-button>

 </div>
</template>

<script>
import { Toast } from 'vant'
export default {
  name: 'manager',
  data() {
    return {
      loading: false,
      show: false,
      actions: [],
      fromVersionName: '',
      fromVersionCode: '',
      versionName: '',
      versionCode: '',
      appType: '',
      buildType: '',
      isPatch: true,
      patchFile: ''
    }
  },
  mounted() {
    this.getAppTypes()
    const { fromFile, toFile,patchFile } = this.$route.query;
    if (fromFile && toFile && patchFile) {
      this.fromVersionName = fromFile.substring(fromFile.indexOf('V')+1,fromFile.indexOf('_',fromFile.indexOf('V')+1));
      this.fromVersionCode = fromFile.substring(fromFile.indexOf('00_')+3,fromFile.indexOf('.apk'));
      this.versionName = toFile.substring(toFile.indexOf('V')+1,toFile.indexOf('_',toFile.indexOf('V')+1));
      this.versionCode = toFile.substring(toFile.indexOf('00_')+3,toFile.indexOf('.apk'));
      this.patchFile = patchFile;
    }
  },
  computed: {
    submitEnable() {
      return (
        this.patchFile &&
        this.appType &&
        this.buildType &&
        this.versionCode &&
        this.versionName &&
        this.fromVersionName &&
        this.fromVersionCode
      )
    }
  },
  methods: {
    save() {
      const data = {
        versionName: this.versionName,
        versionCode: this.versionCode,
        fromVersionName: this.fromVersionName,
        fromVersionCode: this.fromVersionCode,
        appType: this.appType,
        buildType: this.buildType,
        isPatch: true,
        patch: this.patchFile
      }
      this.$axios.post('/api/savePatch', data).then(res => {
        this.loading = false
        if (res.data.result == 'success') {
          Toast('保存成功')
        } else {
          Toast(res.data.msg)
        }
      })
    },
    getAppTypes() {
      this.$axios.get('/api/appTypes').then(res => {
        if (res.data.result == 'success') {
          this.actions = res.data.data.map(d => {
            return {
              name: d,
              callback: () => {
                this.show = false
                this.appType = d
              }
            }
          })
          if (this.actions.length > 0) {
            this.appType = this.actions[0].name
          }
        } else {
          console.log(res.data.msg)
        }
      })
    }
  }
}
</script>

<style scoped>
.mtop10 {
  margin-top: 10px;
}
</style>
